package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.FreemarkUtil;
import cn.wolfcode.trip.base.domain.Reply;
import cn.wolfcode.trip.base.domain.Review;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.ReviewQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IReplyService;
import cn.wolfcode.trip.base.service.IReviewService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//区域资源的控制器
@RestController
@RequestMapping("/reviews")
public class ReviewController {
    @Autowired
    private IReviewService reviewservice;
    @Autowired
    private ServletContext ctx;
    @Autowired
    private IReplyService replyService;

    /*
    获取所有的区域
    资源:/reviews
    动作:GET
    参数:
     */
    @PostMapping
    public Object saveReview(Review entity){
        reviewservice.saveOrUpdate(entity);
        return new JSONResult();
    }

    /*
    获取某个区域推荐的攻略
    资源:/reviews
    动作:GET
    参数:state=2&essayId=3
     */
    @GetMapping
    public Object queryReviews(ReviewQueryObject qo){
        qo.setPageSize(4);
        PageInfo<Review> pageInfo = reviewservice.query(qo);
        return  pageInfo;
    }

    /*
    获取某个文章的所有评论
    资源:/reviews//{beReplyId}/replies
    动作:POST
    参数:state=2&essayId=3
     */
    @PostMapping("/{beReplyId}/replies")
    public Object saveReply(Reply entity){
        replyService.saveOrUpdate(entity);
        return  new JSONResult();
    }

    /**
     * 查询评论当前用户的所有评论
     * @param qo
     */
    @GetMapping("/user")
    public Object reviewUser(ReviewQueryObject qo) {
        List<Review> list=null;
       if(qo.getState()==1){
           list=reviewservice.getTravel(qo);
       }else if(qo.getState()==2){
           list=reviewservice.getComment(qo);
       }
        return list;
    }

    /**
     * 修改已读未读状态
     * @param qo
     */
    @PutMapping("/readed")
    public void update(ReviewQueryObject qo) {
        reviewservice.update(qo);
    }
}
